﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class CreatorSpriteAsset
{
    [MenuItem("Tools/Static Sprite Asset", false, 10)]
    static void CreatorStaticAsset()
    {

        Object target = Selection.activeObject;
        if (target == null || target.GetType() != typeof(Texture2D))
            return;

        Texture2D sourceTex = target as Texture2D;
        //整体路径
        string filePathWithName = AssetDatabase.GetAssetPath(sourceTex);
        //带后缀的文件名
        string fileNameWithExtension = Path.GetFileName(filePathWithName);
        //不带后缀的文件名
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePathWithName);
        //不带文件名的路径
        string filePath = filePathWithName.Replace(fileNameWithExtension, "");

        SpriteAssets spriteAsset = AssetDatabase.LoadAssetAtPath(filePath + fileNameWithoutExtension + ".asset", typeof(SpriteAssets)) as SpriteAssets;
        bool isNewAsset = spriteAsset == null ? true : false;
        if (isNewAsset)
        {
            spriteAsset = ScriptableObject.CreateInstance<SpriteAssets>();
            spriteAsset.sourceTexture = sourceTex;
            spriteAsset.IsStatic = true;
            spriteAsset.Id = 0;
            spriteAsset.groupList = GetAssetSpriteInfor(sourceTex, true);
            AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset");
        }

    }
    [MenuItem("Tools/Dynamic Sprite Asset", false, 11)]
    static void CreatorDynamicAsset()
    {

        Object target = Selection.activeObject;
        if (target == null || target.GetType() != typeof(Texture2D))
            return;

        Texture2D sourceTex = target as Texture2D;
        //整体路径
        string filePathWithName = AssetDatabase.GetAssetPath(sourceTex);
        //带后缀的文件名
        string fileNameWithExtension = Path.GetFileName(filePathWithName);
        //不带后缀的文件名
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePathWithName);
        //不带文件名的路径
        string filePath = filePathWithName.Replace(fileNameWithExtension, "");

        SpriteAssets spriteAsset = AssetDatabase.LoadAssetAtPath(filePath + fileNameWithoutExtension + ".asset", typeof(SpriteAssets)) as SpriteAssets;
        bool isNewAsset = spriteAsset == null ? true : false;
        if (isNewAsset)
        {
            spriteAsset = ScriptableObject.CreateInstance<SpriteAssets>();
            spriteAsset.sourceTexture = sourceTex;
            spriteAsset.IsStatic = false;
            spriteAsset.Id = 1;
            spriteAsset.groupList = GetAssetSpriteInfor(sourceTex, false);
            AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset");
        }

    }
    public static List<SpriteInfoGroup> GetAssetSpriteInfor(Texture sourceTextrue, bool isStatic)
    {
        List<SpriteInfoGroup> _listGroup = new List<SpriteInfoGroup>();
        string filePath = UnityEditor.AssetDatabase.GetAssetPath(sourceTextrue);

        Object[] objects = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(filePath);

        List<SpriteInfo> _tempSprite = new List<SpriteInfo>();

        Vector2 _texSize = new Vector2(sourceTextrue.width, sourceTextrue.height);
        for (int i = 0; i < objects.Length; i++)
        {
            if (objects[i].GetType() != typeof(Sprite))
                continue;
            SpriteInfo temp = new SpriteInfo();
            Sprite sprite = objects[i] as Sprite;
            temp.Id = i;
            temp.Name = sprite.name;
            temp.Pivot = sprite.pivot;
            temp.Rect = sprite.rect;
            temp.Sprite = sprite;
            temp.Tag = sprite.name;
            temp.UV = GetSpriteUV(_texSize, sprite.rect);
            _tempSprite.Add(temp);
        }
        for (int i = 0; i < _tempSprite.Count; i++)
        {
            SpriteInfoGroup _tempGroup = new SpriteInfoGroup();
            if (!isStatic)
            {
                string tag = _tempSprite[i].Tag;
                string[] s = tag.Split('_');
                _tempGroup.Tag = s[0];
                _tempGroup.ListSpriteInfos = new List<SpriteInfo>();
                _tempGroup.ListSpriteInfos.Add(_tempSprite[i]);
                for (int j = i + 1; j < _tempSprite.Count; j++)
                {
                    if (_tempSprite[j].Tag.Contains(_tempGroup.Tag))
                    {
                        _tempGroup.ListSpriteInfos.Add(_tempSprite[j]);
                        _tempSprite.RemoveAt(j);
                        j--;
                    }
                }
                _listGroup.Add(_tempGroup);
                _tempSprite.RemoveAt(i);
                i--;
            }
            else
            {
                _tempGroup.Tag = _tempSprite[i].Tag;
                _tempGroup.ListSpriteInfos = new List<SpriteInfo>();
                _tempGroup.ListSpriteInfos.Add(_tempSprite[i]);
                _listGroup.Add(_tempGroup);
            }

            //_tempGroup.Size = 24.0f;
            //_tempGroup.Width = 1.0f;

        }

        return _listGroup;
    }
    static Vector2[] GetSpriteUV(Vector2 texSize, Rect _sprRect)
    {
        Vector2[] uv = new Vector2[4];
        uv[0] = new Vector2(_sprRect.x / texSize.x, (_sprRect.y + _sprRect.height) / texSize.y);
        uv[1] = new Vector2((_sprRect.x + _sprRect.width) / texSize.x, (_sprRect.y + _sprRect.height) / texSize.y);
        uv[2] = new Vector2((_sprRect.x + _sprRect.width) / texSize.x, _sprRect.y / texSize.y);
        uv[3] = new Vector2(_sprRect.x / texSize.x, _sprRect.y / texSize.y);
        return uv;
    }
}
